package hhm.android.main

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import hhm.android.library.BottomDataAnalyseDateSelectSheet
import hhm.android.library.GetDateBeanListener
import hhm.android.library.calendar.bean.DateBean
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.main.databinding.ActivityDataAnalyseBinding
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.base.analyseDataBeforeSet
import siau.android.base.enum.HeightClass
import siau.android.base.enum.WeightClass
import siau.android.http.model.GetAnalyseDateRequest
import siau.android.http.model.GetAnalyseDateResponse
import siau.android.http.model.GetAnalyseDatesRequest
import siau.android.library.KeyString
import java.text.SimpleDateFormat

class DataAnalyseActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityDataAnalyseBinding
    private var pageType = 1
    var childId = -1
    var currentDate: Long = 0
    var bottomDataAnalyseDateSelectSheet: BottomDataAnalyseDateSelectSheet? = null
    var rightCla = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_analyse)

        pageType = intent.getIntExtra(KeyString.type, 1)
        childId = intent.getIntExtra(KeyString.childId, -1)

        var cla = ""
        if (pageType == OneFragmentInner.PAGE_STATE_WEIGHT) {
            when (SBApplication.userData.weightUnit) {
                WeightClass.KG.msg -> {
                    rightCla = getString(R.string.class_kg)
                    cla = "(kg)"
                }
                WeightClass.JIN.msg -> {
                    rightCla = getString(R.string.class_jin)
                    cla = "(斤)"
                }
                WeightClass.LB.msg -> {
                    rightCla = getString(R.string.class_bang)
                    cla = "(lb)"
                }
            }
        } else {
            when (SBApplication.userData.heightUnit) {
                HeightClass.CM.msg -> {
                    rightCla = getString(R.string.class_cm)
                    cla = "(cm)"
                }
                HeightClass.FT.msg -> {
                    rightCla = getString(R.string.class_ft)
                    cla = "(ft)"
                }
            }
        }

        if (pageType == 1) {
            initTitleWithLine(getString(R.string.weight_analyse))
            dataBinding.activityDataAnalyseSimple.text = getString(R.string.simple_weight) + cla
            dataBinding.activityDataAnalyseMy.text = getString(R.string.my_weight) + cla
        } else {
            initTitleWithLine(getString(R.string.height_analyse))
            dataBinding.activityDataAnalyseSimple.text = getString(R.string.simple_height) + cla
            dataBinding.activityDataAnalyseMy.text = getString(R.string.my_height) + cla
        }

        dataBinding.activityDataAnalyseDate.text = "- -"
        dataBinding.activityDataAnalyseSimpleTv.text = "- -"
        dataBinding.activityDataAnalyseMyTv.text = "- -"
        dataBinding.activityDataAnalyseResult.text = "- -"

        showLoadingFragment(R.id.activity_data_analyse_fl)
        getAnalyseListData()

        dataBinding.activityDataAnalyseTop.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                bottomDataAnalyseDateSelectSheet?.show()
            }
        })
    }

    private fun getAnalyseListData() {
        val b = GetAnalyseDatesRequest(
            childId, if (pageType == 1) {
                "WEIGHT"
            } else {
                "HEIGHT"
            }
        )
        HttpHelper().getAnalyseDates(b)?.subscribe({
            bottomDataAnalyseDateSelectSheet = BottomDataAnalyseDateSelectSheet(this, it, object : GetDateBeanListener {
                override fun getDateBean(db: DateBean) {
                    val sdf = SimpleDateFormat("yyyyMMdd")
                    currentDate = sdf.parse(db.date).time
                    showLoadingFragment(R.id.activity_data_analyse_fl)
                    getDateData()
                }
            })

            currentDate = it[0]

            getDateData()
        }, {
            removeLoadingFragment()
            showToast(it.message)
        })
    }

    private fun getDateData() {
        val req = GetAnalyseDateRequest(
            childId, if (pageType == 1) {
                "WEIGHT"
            } else {
                "HEIGHT"
            }, currentDate, currentDate + 86399999
        )
        HttpHelper().getAnalyseDate(req)?.subscribe({ i ->
            removeLoadingFragment()
            setData(i)
        }, { i ->
            removeLoadingFragment()
            showToast(i.message)
        })
    }

    private fun setData(i: GetAnalyseDateResponse) {
        val sdf = SimpleDateFormat("yyyy/MM/dd")
        dataBinding.activityDataAnalyseDate.text = sdf.format(i.inquireStartTime)
        dataBinding.activityDataAnalyseSimpleTv.text = analyseDataBeforeSet(i.intervalList[0].toString(), pageType, rightCla) + "~" + analyseDataBeforeSet(i.intervalList[1].toString(), pageType, rightCla)
        var resultData = analyseDataBeforeSet(i.difference.toString(), pageType, rightCla)
        if (!resultData.startsWith("-")) {
            resultData = "+$resultData"
        }
        dataBinding.activityDataAnalyseResult.text = resultData
        dataBinding.activityDataAnalyseTime.text = sdf.format(currentDate)
        if (pageType == 1) {
            dataBinding.activityDataAnalyseMyTv.text = analyseDataBeforeSet(i.weight.toString(), pageType, rightCla)
        } else {
            dataBinding.activityDataAnalyseMyTv.text = analyseDataBeforeSet(i.height.toString(), pageType, rightCla)
        }
    }
}